package cn.jarlen.richcommon2.shapeimageview;

import android.content.Intent;
import android.view.View;

import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Random;

import cn.jarlen.richcommon.ui.BaseActivity;
import cn.jarlen.richcommon.widget.CircleImageView;
import cn.jarlen.richcommon2.R;

import static cn.jarlen.richcommon2.shapeimageview.TestCircleImageActivity.urls;

public class TestCircleImageView2Activity extends BaseActivity implements View.OnClickListener {

    private CircleImageView circleImageView;
    private String url = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test_circle_image_view2;
    }

    @Override
    protected void onBindView() {
        Intent intent = getIntent();
        if (intent == null) {
            finish();
            return;
        }
        EventBus.getDefault().register(this);

        url = intent.getStringExtra("url");
        circleImageView = (CircleImageView) findViewById(R.id.iv_circle);
        findViewById(R.id.testBtn).setOnClickListener(this);
        Glide.with(this)
                .load(url)
                .fitCenter()
                .placeholder(R.drawable.shape_img_default)
                .into(circleImageView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.testBtn:
                int nums = new Random().nextInt(4);
                url = urls[nums];
                EventBus.getDefault().post(new CircleUpdateEvent(url));
                break;
            default:

                break;
        }
    }

    @Subscribe
    public void updateCircle(CircleUpdateEvent event) {
        url = event.getUrl();
        Glide.with(this)
                .load(url)
                .fitCenter()
                .placeholder(R.drawable.shape_img_default)
                .into(circleImageView);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
